package com.feeyin.radar.interceptor;

import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.filter.LoginFilter;
import com.feeyin.radar.sys.model.ModelLog;
import com.feeyin.radar.sys.service.ModelLogService;
import java.lang.reflect.Method;
import java.util.Date;
import org.springframework.aop.AfterReturningAdvice;

public class AfterAdviceInterceptor
  implements AfterReturningAdvice
{
  private ModelLogService modelLogService;

  public ModelLogService getModelLogService()
  {
    return this.modelLogService;
  }

  public void setModelLogService(ModelLogService modelLogService) {
    this.modelLogService = modelLogService;
  }

  public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable
  {
    Integer userId = LoginFilter.getThreadLocalUserId();
    String methodName = arg1.getName();
    if ((userId != null) && (!StringUtils.isEmpty(methodName)))
      addModelLog(userId, methodName, arg2);
  }

  private void addModelLog(Integer userId, String methodName, Object[] arg2)
  {
    ModelLog modelLog = new ModelLog();
    modelLog.setLogDate(new Date());
    modelLog.setUserId(userId);

    addModelLogByCustomerManage(modelLog, methodName, arg2);
    addModelLogBySaleProcess(modelLog, methodName, arg2);
    addModelLogBySaleFinance(modelLog, methodName, arg2);
    addModelLogByTeamManage(modelLog, methodName, arg2);

    if ((modelLog.getFunctionId() != null) && 
      (!StringUtils.isEmpty(modelLog.getFunctionOperate())) && 
      (modelLog.getParentFunctionId() != null))
      this.modelLogService.insertModelLog(modelLog);
  }

  private void addModelLogByCustomerManage(ModelLog modelLog, String methodName, Object[] arg2)
  {
    if (methodName.equals("protectCustomer")) {
      modelLog.setFunctionId(Integer.valueOf(51));
      modelLog.setFunctionOperate("6");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("batchImportCustomer")) {
      if ((arg2 != null) && (arg2.length > 0)) {
        int sign = ((Integer)arg2[4]).intValue();
        if (sign == 0)
          modelLog.setFunctionId(Integer.valueOf(51));
        else if (sign == 1)
          modelLog.setFunctionId(Integer.valueOf(83));
        else if (sign == 2) {
          modelLog.setFunctionId(Integer.valueOf(102));
        }
        modelLog.setFunctionOperate("5");
        modelLog.setParentFunctionId(Integer.valueOf(5));
      }
    } else if (methodName.equals("insertCustomer")) {
      modelLog.setFunctionId(Integer.valueOf(52));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("updateCustomer")) {
      modelLog.setFunctionId(Integer.valueOf(52));
      modelLog.setFunctionOperate("2");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("discardMyCustomers")) {
      modelLog.setFunctionId(Integer.valueOf(52));
      modelLog.setFunctionOperate("4");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("restoreCustomerInfo")) {
      modelLog.setFunctionId(Integer.valueOf(52));
      modelLog.setFunctionOperate("7");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("insertCustomerShare")) {
      modelLog.setFunctionId(Integer.valueOf(53));
      modelLog.setFunctionOperate("10");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("deleteCustomerShare")) {
      modelLog.setFunctionId(Integer.valueOf(53));
      modelLog.setFunctionOperate("3");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("insertLinkman")) {
      modelLog.setFunctionId(Integer.valueOf(54));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("updateLinkman")) {
      modelLog.setFunctionId(Integer.valueOf(54));
      modelLog.setFunctionOperate("2");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("restoreLinkmanInfo")) {
      modelLog.setFunctionId(Integer.valueOf(54));
      modelLog.setFunctionOperate("7");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    } else if (methodName.equals("deleteCustomer")) {
      modelLog.setFunctionId(Integer.valueOf(57));
      modelLog.setFunctionOperate("3");
      modelLog.setParentFunctionId(Integer.valueOf(5));
    }
  }

  private void addModelLogBySaleProcess(ModelLog modelLog, String methodName, Object[] arg2)
  {
    if (methodName.equals("insertSaleRecord")) {
      modelLog.setFunctionId(Integer.valueOf(62));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(6));
    } else if (methodName.equals("updateSaleRecord")) {
      modelLog.setFunctionId(Integer.valueOf(62));
      modelLog.setFunctionOperate("2");
      modelLog.setParentFunctionId(Integer.valueOf(6));
    }
  }

  private void addModelLogBySaleFinance(ModelLog modelLog, String methodName, Object[] arg2)
  {
    if (methodName.equals("auditContract")) {
      modelLog.setFunctionId(Integer.valueOf(71));
      modelLog.setFunctionOperate("11");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("returnContract")) {
      modelLog.setFunctionId(Integer.valueOf(71));
      modelLog.setFunctionOperate("12");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("auditSaleCost")) {
      modelLog.setFunctionId(Integer.valueOf(72));
      modelLog.setFunctionOperate("11");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("returnSaleCost")) {
      modelLog.setFunctionId(Integer.valueOf(72));
      modelLog.setFunctionOperate("12");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("insertSaleCost")) {
      modelLog.setFunctionId(Integer.valueOf(73));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("updateSaleCost")) {
      modelLog.setFunctionId(Integer.valueOf(73));
      modelLog.setFunctionOperate("2");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("deleteSaleCost")) {
      modelLog.setFunctionId(Integer.valueOf(73));
      modelLog.setFunctionOperate("3");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("insertContract")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("updateContract")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("2");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("stopContract")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("13");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("cancelContract")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("15");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("insertInvoice")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("14");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("importContract")) {
      modelLog.setFunctionId(Integer.valueOf(74));
      modelLog.setFunctionOperate("5");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    } else if (methodName.equals("insertBackCost")) {
      modelLog.setFunctionId(Integer.valueOf(75));
      modelLog.setFunctionOperate("1");
      modelLog.setParentFunctionId(Integer.valueOf(7));
    }
  }

  private void addModelLogByTeamManage(ModelLog modelLog, String methodName, Object[] arg2)
  {
    if (methodName.equals("mergeCustomer")) {
      modelLog.setFunctionId(Integer.valueOf(84));
      modelLog.setFunctionOperate("16");
      modelLog.setParentFunctionId(Integer.valueOf(8));
    }
  }
}